package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.post.api.SysPositionService;
import com.nidu.demo.post.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 职位管理 Controller
 */
@Tag(name = "管理后台 - 职位")
@RestController
@RequestMapping("/system/post")
@Validated
@RequiredArgsConstructor
public class SysPositionController {

    private final SysPositionService service;

    @Operation(summary = "创建职位", description = "创建新的职位")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:post:create')")
    public SingleResponse<PositionCO> addPosition(@RequestBody PositionAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新职位", description = "更新职位的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:post:update')")
    public SingleResponse<PositionCO> updatePosition(@PathVariable Long id, @RequestBody PositionUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除职位", description = "根据 ID 删除职位")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:post:delete')")
    public Response deletePosition(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除职位", description = "根据 ID 列表删除多个职位")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:post:delete')")
    public Response deletePositions(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取职位详情", description = "根据 ID 获取职位详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public SingleResponse<PositionCO> getPositionById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询职位列表", description = "获取所有职位信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public MultiResponse<PositionCO> listPositions(PositionListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "分页查询职位", description = "分页获取职位信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:post:query')")
    public PageResponse<PositionCO> pagePositions(PositionPageQry qry) {
        return service.page(qry);
    }
}